package com.nytimes.ohos.store.sample.data.model;

import com.google.common.base.MoreObjects;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import java.util.List;

/**
 * Immutable implementation of {@link Post}.
 * <p>
 * Use the builder to create immutable instances:
 * {@code ImmutablePost.builder()}.
 */
@SuppressWarnings("all")
public final class ImmutablePost extends Post {
  private final  Preview preview;
  private final String title;
  private final String url;
  private final  Integer height;
  private final  Integer width;
  private final Optional<Image> nestedThumbnail;

  private ImmutablePost(
       Preview preview,
      String title,
      String url,
       Integer height,
       Integer width) {
    this.preview = preview;
    this.title = title;
    this.url = url;
    this.height = height;
    this.width = width;
    this.nestedThumbnail = Preconditions.checkNotNull(super.nestedThumbnail(), "nestedThumbnail");
  }

  /**
   * Preview
   *
   * @return The value of the {@code preview} attribute
   */
  @Override
  public  Preview preview() {
    return preview;
  }

  /**
   * title
   *
   * @return The value of the {@code title} attribute
   */
  @Override
  public String title() {
    return title;
  }

  /**
   * url
   *
   * @return The value of the {@code url} attribute
   */
  @Override
  public String url() {
    return url;
  }

  /**
   * height
   *
   * @return The value of the {@code height} attribute
   */
  @Override
  public  Integer height() {
    return height;
  }

  /**
   * width
   *
   * @return The value of the {@code width} attribute
   */
  @Override
  public  Integer width() {
    return width;
  }

  /**
   * nestedThumbnail
   *
   * @return The computed-at-construction value of the {@code nestedThumbnail} attribute
   */
  @Override
  public Optional<Image> nestedThumbnail() {
    return nestedThumbnail;
  }

  /**
   * Copy the current immutable object by setting a value for the {@link Post#preview() preview} attribute.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   *
   * @param preview A new value for preview (can be {@code null})
   * @return A modified copy of the {@code this} object
   */
  public final ImmutablePost withPreview( Preview preview) {
    if (this.preview == preview) return this;
    return new ImmutablePost(preview, this.title, this.url, this.height, this.width);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link Post#title() title} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   *
   * @param title A new value for title
   * @return A modified copy of the {@code this} object
   */
  public final ImmutablePost withTitle(String title) {
    if (this.title.equals(title)) return this;
    String newValue = Preconditions.checkNotNull(title, "title");
    return new ImmutablePost(this.preview, newValue, this.url, this.height, this.width);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link Post#url() url} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   *
   * @param url A new value for url
   * @return A modified copy of the {@code this} object
   */
  public final ImmutablePost withUrl(String url) {
    if (this.url.equals(url)) return this;
    String newValue = Preconditions.checkNotNull(url, "url");
    return new ImmutablePost(this.preview, this.title, newValue, this.height, this.width);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link Post#height() height} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   *
   * @param height A new value for height (can be {@code null})
   * @return A modified copy of the {@code this} object
   */
  public final ImmutablePost withHeight( Integer height) {
    if (Objects.equal(this.height, height)) return this;
    return new ImmutablePost(this.preview, this.title, this.url, height, this.width);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link Post#width() width} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   *
   * @param width A new value for width (can be {@code null})
   * @return A modified copy of the {@code this} object
   */
  public final ImmutablePost withWidth( Integer width) {
    if (Objects.equal(this.width, width)) return this;
    return new ImmutablePost(this.preview, this.title, this.url, this.height, width);
  }

  /**
   * This instance is equal to all instances of {@code ImmutablePost} that have equal attribute values.
   *
   * @param another Object
   * @return {@code true} if {@code this} is equal to {@code another} instance
   */
  @Override
  public boolean equals(Object another) {
    if (this == another) return true;
    return another instanceof ImmutablePost
        && equalTo((ImmutablePost) another);
  }

  private boolean equalTo(ImmutablePost another) {
    return Objects.equal(preview, another.preview)
        && title.equals(another.title)
        && url.equals(another.url)
        && Objects.equal(height, another.height)
        && Objects.equal(width, another.width)
        && nestedThumbnail.equals(another.nestedThumbnail);
  }

  /**
   * Computes a hash code from attributes: {@code preview}, {@code title}, {@code url}, {@code height}, {@code width}, {@code nestedThumbnail}.
   *
   * @return hashCode value
   */
  @Override
  public int hashCode() {
    int h = 31;
    h = h * 17 + Objects.hashCode(preview);
    h = h * 17 + title.hashCode();
    h = h * 17 + url.hashCode();
    h = h * 17 + Objects.hashCode(height);
    h = h * 17 + Objects.hashCode(width);
    h = h * 17 + nestedThumbnail.hashCode();
    return h;
  }

  /**
   * Prints the immutable value {@code Post} with attribute values.
   *
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    return MoreObjects.toStringHelper("Post")
        .omitNullValues()
        .add("preview", preview)
        .add("title", title)
        .add("url", url)
        .add("height", height)
        .add("width", width)
        .add("nestedThumbnail", nestedThumbnail)
        .toString();
  }

  /**
   * Creates an immutable copy of a {@link Post} value.
   * Uses accessors to get values to initialize the new immutable instance.
   * If an instance is already immutable, it is returned as is.
   *
   * @param instance The instance to copy
   * @return A copied immutable Post instance
   */
  public static ImmutablePost copyOf(Post instance) {
    if (instance instanceof ImmutablePost) {
      return (ImmutablePost) instance;
    }
    return ImmutablePost.builder()
        .from(instance)
        .build();
  }

  /**
   * Creates a builder for {@link ImmutablePost ImmutablePost}.
   *
   * @return A new ImmutablePost builder
   */
  public static Builder builder() {
    return new Builder();
  }

  /**
   * Builds instances of type {@link ImmutablePost ImmutablePost}.
   * Initialize attributes and then invoke the {@link #build()} method to create an
   * immutable instance.
   * <p><em>{@code Builder} is not thread-safe and generally should not be stored in a field or collection,
   * but instead used immediately to create instances.</em>
   */
  public static final class Builder {
    private static final long INIT_BIT_TITLE = 0x1L;
    private static final long INIT_BIT_URL = 0x2L;
    private long initBits = 0x3L;

    private Preview preview;
    private String title;
    private String url;
    private Integer height;
    private Integer width;

    private Builder() {
    }

    /**
     * Fill a builder with attribute values from the provided {@code Post} instance.
     * Regular attribute values will be replaced with those from the given instance.
     * Absent optional values will not replace present values.
     *
     * @param instance The instance from which to copy values
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder from(Post instance) {
      Preconditions.checkNotNull(instance, "instance");
       Preview previewValue = instance.preview();
      if (previewValue != null) {
        preview(previewValue);
      }
      title(instance.title());
      url(instance.url());
       Integer heightValue = instance.height();
      if (heightValue != null) {
        height(heightValue);
      }
       Integer widthValue = instance.width();
      if (widthValue != null) {
        width(widthValue);
      }
      return this;
    }

    /**
     * Initializes the value for the {@link Post#preview() preview} attribute.
     *
     * @param preview The value for preview (can be {@code null})
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder preview( Preview preview) {
      this.preview = preview;
      return this;
    }

    /**
     * Initializes the value for the {@link Post#title() title} attribute.
     *
     * @param title The value for title 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder title(String title) {
      this.title = Preconditions.checkNotNull(title, "title");
      initBits &= ~INIT_BIT_TITLE;
      return this;
    }

    /**
     * Initializes the value for the {@link Post#url() url} attribute.
     *
     * @param url The value for url 
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder url(String url) {
      this.url = Preconditions.checkNotNull(url, "url");
      initBits &= ~INIT_BIT_URL;
      return this;
    }

    /**
     * Initializes the value for the {@link Post#height() height} attribute.
     *
     * @param height The value for height (can be {@code null})
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder height( Integer height) {
      this.height = height;
      return this;
    }

    /**
     * Initializes the value for the {@link Post#width() width} attribute.
     *
     * @param width The value for width (can be {@code null})
     * @return {@code this} builder for use in a chained invocation
     */
    public final Builder width( Integer width) {
      this.width = width;
      return this;
    }

    /**
     * Builds a new {@link ImmutablePost ImmutablePost}.
     *
     * @return An immutable instance of Post
     * @throws IllegalStateException if any required attributes are missing
     */
    public ImmutablePost build() {
      if (initBits != 0) {
        throw new IllegalStateException(formatRequiredAttributesMessage());
      }
      return new ImmutablePost(preview, title, url, height, width);
    }

    private String formatRequiredAttributesMessage() {
      List<String> attributes = Lists.newArrayList();
      if ((initBits & INIT_BIT_TITLE) != 0) attributes.add("title");
      if ((initBits & INIT_BIT_URL) != 0) attributes.add("url");
      return "Cannot build Post, some of required attributes are not set " + attributes;
    }
  }
}
